package com.foundersoftware.commons.organization.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.foundersoftware.commons.organization.ObjectAlreadyExistsException;
import com.foundersoftware.commons.organization.ObjectNotFoundException;
import com.foundersoftware.commons.organization.PersistenceException;
import com.foundersoftware.commons.organization.dao.IOrganizationDAO;
import com.foundersoftware.commons.organization.model.AbstractOrganizationObject;
import com.foundersoftware.commons.organization.model.Employee;
import com.foundersoftware.commons.organization.model.OrganizationUnit;
import com.foundersoftware.commons.organization.model.OrganizationUnitType;
import com.foundersoftware.commons.organization.model.Position;
import com.foundersoftware.commons.organization.model.PositionType;
import com.foundersoftware.commons.organization.model.Responsibility;
import com.foundersoftware.commons.organization.model.Role;
import com.foundersoftware.commons.organization.model.RoleType;
import com.foundersoftware.commons.organization.model.Type;
import com.foundersoftware.commons.organization.model.WorkGroup;
import com.foundersoftware.commons.organization.model.WorkGroupType;


public class RAMOrganizationStore implements IOrganizationDAO {
    /**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	private HashMap employees = new HashMap(200);
    private HashMap orgUnits = new HashMap(50);
    private HashMap orgUnitTypes = new HashMap(20);
    private HashMap positions = new HashMap(250);
    private HashMap positionTypes = new HashMap(10);
    private HashMap roles = new HashMap(100);
    private HashMap roleTypes = new HashMap(10);
    private HashMap workGroups = new HashMap(50);
    private HashMap workGroupTypes = new HashMap(10);
    private HashMap responsibilities = new HashMap(500);

    private HashMap employeesOfOrgUnit = new HashMap(50);
    private HashMap employeesOfRole = new HashMap(100);
    private HashMap employeesOfPosition = new HashMap(250);
    private HashMap employeesOfWorkGroup = new HashMap(50);
    private HashMap orgUnitsOfOrgUnitType = new HashMap(20);
    private HashMap positionsOfPositionType = new HashMap(20);
    private HashMap positionsOfOrgUnit = new HashMap(50);
    private HashMap childOrgUnitsOfOrgUnit = new HashMap(50);
    private HashMap workGroupsOfWorkGroupType = new HashMap(10);
    private HashMap childWorkGroupOfWorkGroup = new HashMap(50);
    private HashMap rolesOfRoleType= new HashMap(20);
    private HashMap rolesOfWorkGroup = new HashMap(50);
    private HashMap responsibilitiesOfPosition = new HashMap(250);
    private HashMap responsibilitiesOfRole = new HashMap(100);

    private HashMap<String, Serializable> locks = new HashMap<String,Serializable>(10);

    private  HashMap ramDataBase = new HashMap(18);




    public RAMOrganizationStore() {
        initialize();
    }

    private void initialize() {

     ramDataBase.put(Employee.class.getName(),employees);
     ramDataBase.put(OrganizationUnitType.class.getName(),orgUnitTypes);
     ramDataBase.put(OrganizationUnit.class.getName(),orgUnits);
     ramDataBase.put(PositionType.class.getName(),positionTypes);
     ramDataBase.put(Position.class.getName(),positions);
     ramDataBase.put(RoleType.class.getName(),roleTypes);
     ramDataBase.put(Role.class.getName(),roles);
     ramDataBase.put(WorkGroupType.class.getName(),workGroupTypes);
     ramDataBase.put(WorkGroup.class.getName(),workGroups);
     ramDataBase.put(Responsibility.class.getName(),responsibilities);

     locks.put(Employee.class.getName(),new Serializable(){});
     locks.put(OrganizationUnitType.class.getName(),new Serializable(){});
     locks.put(OrganizationUnit.class.getName(),new Serializable(){});
     locks.put(PositionType.class.getName(),new Serializable(){});
     locks.put(Position.class.getName(), new Serializable(){});
     locks.put(RoleType.class.getName(), new Serializable(){});
     locks.put(Role.class.getName(),new Serializable(){});
     locks.put(WorkGroupType.class.getName(),new Serializable(){});
     locks.put(WorkGroup.class.getName(),new Serializable(){});
     locks.put(Responsibility.class.getName(),new Serializable(){});
    }
    public void storeEmployee(Employee employee, boolean isCreated) throws
            ObjectAlreadyExistsException,
            PersistenceException {
            storeOrganizationObject(employee,isCreated);
   }

        public void storeOrganizationUnit(OrganizationUnit organizationUnit,
                                          boolean isCreated) throws
                ObjectAlreadyExistsException, PersistenceException {
             storeOrganizationObject(organizationUnit,isCreated);
        }

        public void storeOrganizationUnitType(OrganizationUnitType
                                              organizationUnitType,
                                              boolean isCreated) throws
                PersistenceException, ObjectAlreadyExistsException {
               storeOrganizationObject(organizationUnitType,isCreated);
        }

        public void storePosition(Position position, boolean isCreated) throws
                PersistenceException, ObjectAlreadyExistsException {
               storeOrganizationObject(position,isCreated);
        }

        public void storePositionType(PositionType positionType,
                                      boolean isCreated) throws
                PersistenceException, ObjectAlreadyExistsException {
               storeOrganizationObject(positionType,isCreated);
        }

        public void storeResponsibility(Responsibility responsibility,
                                        boolean isCreated) throws
                PersistenceException, ObjectAlreadyExistsException {
               storeOrganizationObject(responsibility,isCreated);
        }

        public void storeRole(Role role, boolean isCreated) throws
                PersistenceException, ObjectAlreadyExistsException {
                storeOrganizationObject(role,isCreated);
        }

        public void storeRoleType(RoleType roleType, boolean isCreated) throws
                PersistenceException, ObjectAlreadyExistsException {
               storeOrganizationObject(roleType,isCreated);
        }

        public void storeWorkGroup(WorkGroup workGroup, boolean isCreated) throws
                PersistenceException, ObjectAlreadyExistsException {
                storeOrganizationObject(workGroup,isCreated);
        }

        public void storeWorkGroupType(WorkGroupType workGroupType,
                                       boolean isCreated) throws
                PersistenceException, ObjectAlreadyExistsException {
                storeOrganizationObject(workGroupType,isCreated);
        }

        public void deleteEmployee(Employee employee) throws
                PersistenceException {
              employees.remove(employee.getId());
        }

        public void deleteOrganizationUnit(OrganizationUnit organizationUnit) throws
                PersistenceException {
              orgUnits.remove(organizationUnit.getId());
        }

        public void deleteOrganizationUnitType(OrganizationUnitType
                                               organizationUnitType) throws
                PersistenceException {
            orgUnitTypes.remove(organizationUnitType.getId());
        }

        public void deletePosition(Position position) throws
                PersistenceException {
            positions.remove(position.getId());
        }

        public void deletePositionType(PositionType positionType) throws
                PersistenceException {
            positionTypes.remove(positionType.getId());
        }

        public void deleteResponsibility(Responsibility responsibility) throws
                PersistenceException {
            responsibilities.remove(responsibility.getId());
        }

        public void deleteRole(Role role) throws PersistenceException {
            roles.remove(role.getId());
        }

        public void deleteRoleType(RoleType roleType) throws
                PersistenceException {
            roleTypes.remove(roleType.getId());
        }

        public void deleteWorkGroup(WorkGroup workGroup) throws
                PersistenceException {
            workGroups.remove(workGroup.getId());
        }

        public void deleteWorkGroupType(WorkGroupType workGroupType) throws
                PersistenceException {
            workGroupTypes.remove(workGroupType.getId());

        }

        public Employee getEmployee(String employeeid) throws
                PersistenceException {
            return (Employee)employees.get(employeeid);
        }

        public OrganizationUnitType getOrganizationUnitType(String
                organiztionUnitTypeId) throws PersistenceException {
            return (OrganizationUnitType)orgUnitTypes.get(organiztionUnitTypeId);
        }

        public OrganizationUnit getOrganizationUnit(String organizationUnitId) throws
                PersistenceException {
            return (OrganizationUnit) orgUnits.get(organizationUnitId);
        }

        public PositionType getPostionType(String positionTypeId) throws
                PersistenceException {
            return (PositionType)positions.get(positionTypeId);
        }

        public Position getPosition(String positionId) throws
                PersistenceException {
            return (Position)positions.get(positionId);
        }

        public RoleType getRoleType(String roleTypeId) throws
                PersistenceException {
            return (RoleType)roleTypes.get(roleTypeId);
        }

        public Role getRole(String roleId) throws PersistenceException {
            return (Role)roles.get(roleId);
        }

        public WorkGroupType getWorkGroupType(String workGroupTypeId) throws
                PersistenceException {
            return (WorkGroupType)workGroupTypes.get(workGroupTypeId);
        }

        public WorkGroup getWorkGroup(String workGroupId) throws
                PersistenceException {
            return (WorkGroup)workGroups.get(workGroupId);
        }

        public Responsibility getResponsibility(String responsibilityId) throws
                PersistenceException {
            return (Responsibility)responsibilities.get(responsibilityId);
        }

        public Employee[] getAllEmployees() {
            return (Employee[])employees.values().toArray(new Employee[employees.values().size()]);
        }

        public OrganizationUnitType[] getAllOrganizationUnitTypes() {
            return (OrganizationUnitType[])orgUnitTypes.values().toArray();
        }

        public OrganizationUnit[] getAllOrganizationUnits() {
            return (OrganizationUnit[])orgUnits.values().toArray();
        }

        public PositionType[] getAllPositionTypes() {
            return (PositionType[])positionTypes.values().toArray();
        }

        public Position[] getAllPositions() {
            return (Position[])positions.values().toArray();
        }

        public RoleType[] getAllRoleTypes() {
            return (RoleType[]) roleTypes.values().toArray();
        }

        public Role[] getAllRoles() {
            return (Role[]) roles.values().toArray();
        }

        public WorkGroupType[] getAllWorkGroupTypes() {
            return (WorkGroupType[]) workGroupTypes.values().toArray();
        }

        public WorkGroup[] getAllWorkGroups() {
            return (WorkGroup[])workGroups.values().toArray();
        }

        public Responsibility[] getAllResponsibilities() {
            return null;
        }

        public Employee[] getEmployeesOfOrganizationUnit(OrganizationUnit
                organizationUnit, boolean isDirect) {
            return null;
        }

        public OrganizationUnit[] getChildOrganizationUnits(OrganizationUnit
                superOrganizationUnit) {
            return null;
        }

        public Position[] getPositionsOfOrganizationUnit(OrganizationUnit
                organizationUnit) {
            return null;
        }

        public Role[] getRolesOfWorkGroup(WorkGroup workGroup) {
            return null;
        }

        public Role[] getRolesOfEmployee(Employee employee, WorkGroup workGroup) {
            return null;
        }

        public Position getPositionsOfEmployee(Employee employee) {
            return null;
        }

        public Responsibility[] getResponsibilitiesOfRole(Role role) {
            return null;
        }

        public Responsibility[] getResponsibilitiesOfPosition(Position position) {
            return null;
        }

        private void storeOrganizationObject(AbstractOrganizationObject absOrgObj,boolean isCreated) throws
                ObjectAlreadyExistsException,PersistenceException {
            synchronized(locks.get(absOrgObj.getClass().getName())) {
                if(((Map)ramDataBase.get(absOrgObj.getClass().getName())).get(absOrgObj.getId())!=null && isCreated) {
                    throw new ObjectAlreadyExistsException(absOrgObj);

                }

                if(((Map)ramDataBase.get(absOrgObj.getClass().getName())).get(absOrgObj.getId()) == null && !isCreated) {
                    throw new ObjectNotFoundException(absOrgObj);
                }
            }
          return;

        }

    public void storeEmployeeWithOgranizationUnitRelation(String employeeId,
            String organizationUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {

    }

    public void storeEmployeeWithWorkGroupRelation(String employeeId,
            String workGroupId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void storeEmployeeWithPositionRelation(String employeeId,
                                                  String positionId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void storeEmployeeWithRoleAndWorkGroup(String employeeId,
                                                  String roleId,
                                                  String workGroupId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void storeOrganizationUnitWithOrganizationUnitTypeRelation(String
            organizationUnitId, String organizationUnitTypeId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void storeOrganizationUnitWithPositionRelation(String
            organizationUnitId, String positionId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void storeOrganizationUnitWithOrganizationUnitRelation(String
            superOrgUnitId, String subOrgUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void storeResponsibilityWithPositonRelation(String responsibilityId,
            String positionId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void storePositionWithPositionTypeRelation(String positionId,
            String positionTypeId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void storeResponsibilityWithRoleRelation(String responsibilityId,
            String roleId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void storeWorkGroupWithWorkGroupRelation(String superWorkGroupId,
            String subWorkGroupId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void storeWorkGroupWithWorkGroupType(String workGroupId,
                                                String workGroupTypeId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void storeRoleWithRoleTypeRelation(String roleId, String roleTypeId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void storeRoleWithWorkGroupRelation(String roleId,
                                               String workGroupId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void storePositionWithOrganizationUnitRelation(String positionId,
            String organizationUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteEmployeeWithOgranizationUnitRelation(String employeeId,
            String organizationUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteEmployeeWithWorkGroupRelation(String employeeId,
            String workGroupId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteEmployeeWithPositionRelation(String employeeId,
            String positionId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteEmployeeWithRoleAndWorkGroup(String employeeId,
            String roleId, String workGroupId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteOrganizationUnitWithOrganizationUnitTypeRelation(String
            organizationUnitId, String organizationUnitTypeId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void deletePositionWithOrganizationUnitRelation(String positionId,
            String organizationUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteOrganizationUnitWithOrganizationUnitRelation(String
            superOrgUnitId, String subOrgUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteResponsibilityWithPositonRelation(String responsibilityId,
            String positionId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deletePositionWithPositionTypeRelation(String positionId,
            String positionTypeId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteResponsibilityWithRoleRelation(String responsibilityId,
            String roleId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteWorkGroupWithWorkGroupRelation(String superWorkGroupId,
            String subWorkGroupId) throws PersistenceException,
            ObjectAlreadyExistsException {
    }

    public void deleteWorkGroupWithWorkGroupType(String workGroupId,
                                                 String workGroupTypeId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void deleteRoleWithRoleTypeRelation(String roleId, String roleTypeId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public void deleteRoleWithWorkGroupRelation(String roleId,
                                                String workGroupId) throws
            PersistenceException, ObjectAlreadyExistsException {
    }

    public boolean isExistsEmployeeWithOgranizationUnitRelation(String
            employeeId, String organizationUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsEmployeeWithWorkGroupRelation(String employeeId,
            String workGroupId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsEmployeeWithPositionRelation(String employeeId,
            String positionId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsEmployeeWithRoleAndWorkGroup(String employeeId,
            String roleId, String workGroupId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsOrganizationUnitWithOrganizationUnitTypeRelation(
            String organizationUnitId, String organizationUnitTypeId) throws
            PersistenceException, ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsPositionWithOrganizationUnitRelation(String
            positionId, String organizationUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsOrganizationUnitWithOrganizationUnitRelation(String
            superOrgUnitId, String subOrgUnitId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsResponsibilityWithPositonRelation(String
            responsibilityId, String positionId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsPositionWithPositionTypeRelation(String positionId,
            String positionTypeId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsResponsibilityWithRoleRelation(String
            responsibilityId, String roleId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsWorkGroupWithWorkGroupRelation(String
            superWorkGroupId, String subWorkGroupId) throws
            PersistenceException, ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsWorkGroupWithWorkGroupType(String workGroupId,
            String workGroupTypeId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsRoleWithRoleTypeRelation(String roleId,
            String roleTypeId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsRoleWithWorkGroupRelation(String roleId,
            String workGroupId) throws PersistenceException,
            ObjectAlreadyExistsException {
        return false;
    }

    public boolean isExistsEmployee(String employeeId) throws
            PersistenceException {
        return false;
    }

    public boolean isExistsOrganizationUnit(String orgUnitId) throws
            PersistenceException {
        return false;
    }

    public boolean isExistsOrganizationUnitType(String orgUnitType) throws
            PersistenceException {
        return false;
    }

	public void deleteResponsibility(AbstractOrganizationObject responsibility) throws PersistenceException {
		// TODO Auto-generated method stub
		
	}

	public void deleteRoleType(Type roleType) throws PersistenceException {
		// TODO Auto-generated method stub
		
	}

	public void storeResponsibility(AbstractOrganizationObject responsibility, boolean isCreated) throws ObjectAlreadyExistsException, PersistenceException {
		// TODO Auto-generated method stub
		
	}

	public void storeRoleType(Type roleType, boolean isCreated) throws ObjectAlreadyExistsException, PersistenceException {
		// TODO Auto-generated method stub
		
	}

}
